package com.stylefeng.guns.modular.finance.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.CostOnlineLoan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CostService;
import com.stylefeng.guns.modular.finance.service.ICostServiceService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 技术服务费控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 15:56:17
 */
@Controller
@RequestMapping("/costService")
public class CostServiceController extends BaseController {

    private String PREFIX = "/finance/costService/";

    @Autowired
    private ICostServiceService costServiceService;

    /**
     * 跳转到技术服务费首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "costService.html";
    }

    /**
     * 跳转到添加技术服务费
     */
    @RequestMapping("/costService_add")
    public String costServiceAdd() {
        return PREFIX + "costService_add.html";
    }

    /**
     * 跳转到修改技术服务费
     */
    @RequestMapping("/costService_update/{costServiceId}")
    public String costServiceUpdate(@PathVariable Integer costServiceId, Model model) {
        CostService costService = costServiceService.selectById(costServiceId);
        model.addAttribute("item",costService);
        LogObjectHolder.me().set(costService);
        return PREFIX + "costService_edit.html";
    }

    /**
     * 获取技术服务费列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<CostService> costServiceEntityWrapper = new EntityWrapper<>();
        costServiceEntityWrapper.like("company", condition);
        List<Map<String, Object>> mapList = costServiceService.selectMaps(costServiceEntityWrapper);
        BigDecimal total = new BigDecimal(0.00);
        for (Map<String, Object> map: mapList) {
            total = total.add((BigDecimal) map.get("amount"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("amount", total);
        mapList.add(0, map);
        return mapList;
    }

    /**
     * 新增技术服务费
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CostService costService) {
        costService.setCreateTime(new Date());
        costServiceService.insert(costService);
        return SUCCESS_TIP;
    }

    /**
     * 删除技术服务费
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer costServiceId) {
        costServiceService.deleteById(costServiceId);
        return SUCCESS_TIP;
    }

    /**
     * 修改技术服务费
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CostService costService) {
        costServiceService.updateById(costService);
        return SUCCESS_TIP;
    }

    /**
     * 技术服务费详情
     */
    @RequestMapping(value = "/detail/{costServiceId}")
    @ResponseBody
    public Object detail(@PathVariable("costServiceId") Integer costServiceId) {
        return costServiceService.selectById(costServiceId);
    }
}
